<style lang="less">
@import "../../../assets/style/photo.less";
</style>

<template>
    <mxq-router-layout>
        <div class="photo">
            <Head slot="header" @headerClick="headerClick($event)">
                <div slot="headerTitle">
                    <span>{{towerName}}杆塔</span>
                </div>
                <div slot="headerRight">
                    <i class="iconfont icon-duigou" v-show="isEnd"/>
                    <!-- <van-icon name="success" v-show="isEnd"></van-icon> -->
                </div>
                <div slot="headerRight" @click.stop="headerClickHome" v-show="isHome">
                    <i class="iconfont icon-shouye"/>
                    <!-- <van-icon class="home" name="home"/> -->
                </div>
            </Head>
            
            
            <span class="iconBox" v-show="isEnd">
                <img
                    class="iconRight"
                    src="../../../assets/img/changguixunshi/icon_rw.png"
                    @click="cancelTask"/>
                <em class="iconText">{{iconText}}</em>
            </span>

            <van-cell-group  v-show="showCell">
                <van-cell title="巡视人员" value="taskPersonnel" />
                <van-cell title="任务结束时间" value="taskEndTime" />
            </van-cell-group>

            <!-- 拍照 start -->
            <take-photo
                :pohtoParam="pohtoParam"
                :photoLists="photoLists"
                :pohtoShow="pohtoShow"
                @quexian_data="initQuanxian"
                :asterisk = "asterisk"
                :isEnd = "isEnd"/>
            <!-- 拍照 end -->
            
            <van-row class="quexian_item">
                <van-col span="24">
                    <p class="photo_item tit_item clearfix">
                        <label for="架空线路缺陷核查"><em>*</em>架空线路缺陷核查</label>
                    </p>
                </van-col>
            </van-row>

            <!-- 八大线路缺陷 开始 -->
            <quexian-list
                :listLeftImg="listLeftImg" 
                :type_arr="type_arr"
                @quexianClick="quexianClick($event)"/>
            <!-- 八大线路缺陷 结束 -->
            
            <!--一键告警-->
            <img src="../../../assets/img/icon_sos.png"
                class="icon_sos"
                @click="getSos"
                alt=""/>
            <div class="btns">
                <van-tabbar>
                    <van-tabbar-item>
                        <van-button size="small" class="btn_l" @click="goYhList">隐患情况</van-button>
                    </van-tabbar-item>
                    <van-tabbar-item>
                        <van-button size="small" class="btn_r" @click="goClArea">测量记录</van-button>
                    </van-tabbar-item>
                </van-tabbar>
            </div>
            <!-- 弹出层 开始 -->
            <div class="pop_up">
                <van-popup v-model="noTimeShow">
                    <span class="pop_txt">
                        当前在此杆塔100米范围内所停留时间为{{stayTime}}秒，不满足巡视任务杆塔180秒停留作业，是否提交巡视不到位作业？
                    </span>
                    <van-button size="small" class="btn_l" @click="noTimeConfirm">是</van-button>
                    <van-button size="small" class="btn_r" @click="Cancel">否</van-button>
                </van-popup>
            </div>
            <!-- 弹出层 结束 -->

            <!-- 不到位0 -->
            <div class="pop_up">
                <van-popup v-model="noPlaceShow" :close-on-click-overlay="false">
                    <span class="pop_txt">
                        是否提交巡视不到位作业？
                    </span>

                    <!-- 拍照 start -->
                    <take-photo
                    :pohtoParam="pohtoParam"
                    :photoLists="photoLists1"
                    :pohtoShow="pohtoShowDW"
                    :isEnd = "isEnd"/>
                    <!-- 拍照 end -->
                    <van-field
                        v-model="reason"
                        class="select_list"
                        icon="arrow"
                        label="原因"
                        placeholder="请选择"
                        @click-icon="showNoPlaceReason"
                        required
                        disabled/>
                    <!-- <van-field v-model="reason" label="原因" placeholder="请输入不到位原因"/> -->
                    <van-button size="small" class="btn_l" @click="noPlaceConfirm">是</van-button>
                    <van-button size="small" class="btn_r" @click="Cancel">否</van-button>
                </van-popup>
            </div>
            <!-- 原因分析选择弹出模态 -->
            <van-popup v-model="noPlaceReasonFlag" position="bottom" :overlay="true">
                <van-picker
                    :columns="noPlaceReasonColumns"
                    :columns-num="1"
                    :show-toolbar="true"
                    @change="noPlaceReasonChange"
                    @confirm="noPlaceReasonConfirm" 
                    @cancel="noPlaceReasonCancel">
                </van-picker>	 
            </van-popup>
        </div>
    </mxq-router-layout>
</template>

<script>
import Head from '../../../components/head'
import TakePhoto from '../../components/taskTake-photo'
import QuexianList from '../../../components/quexian'
import iconHis from '../../../assets/img/changguixunshi/icon_histroy.png'
import { callData,fileData } from '../../../axios/axios'
import { getLocation } from '../../../api/android'
import { getSession } from '../../../libs/cache'
import { 
    Row,
    Col,
    Tabbar,
	TabbarItem,
    Button,
    Icon,
    Cell,
    CellGroup,
    Field,
    Picker,
    Popup,
    Dialog,
    Toast
} from 'vant'

export default {
    components: {
        Head,
        TakePhoto,
        QuexianList,
        [Row.name]: Row,
        [Col.name]: Col,
        [Tabbar.name]: Tabbar,
        [TabbarItem.name]: TabbarItem,
        [Button.name]: Button,
        [Icon.name]: Icon,
        [Cell.name]: Cell,
        [CellGroup.name]: CellGroup,
        [Field.name]: Field,
        [Picker.name]: Picker,
        [Popup.name]: Popup
    },
    data() {
        return {
            towerName: this.$route.query.towerName,
            iconText: '取消任务',     //右侧按钮
            asterisk: [],
            isEnd: true,            //结束杆塔图标/结束任务图标/拍照图标
            showCell: false,        //是否显示该列表-仅限于已完成状态显示
            isHome: false,
            taskPersonnel: '',      //巡视人员
            taskEndTime: this.$route.query.taskEndTime,        //任务结束时间
            // 历史图标
            listLeftImg:[iconHis,iconHis,iconHis,iconHis,iconHis,iconHis,iconHis,iconHis,iconHis],
            // 拍照参数
            pohtoParam: {
                towerTaskId: this.$route.query.xsTowerTaskId,
                towerId: this.$route.query.towerId,
                taskId: this.$route.query.taskId,
                status: this.$route.query.status
            },
            pohtoShow: {
            	type: 'cgxs',
            	img: true
            },
            // 拍照目录
            photoLists: [                       
                {
                    title: "杆塔牌拍照",
                    photo: []
                },
                {
                    title: "杆基拍照",
                    photo: []
                },
                {
                    title: "全塔拍照",
                    photo: []
                },
                {
                    title: "塔头拍照",
                    photo: []
                },
                {
                    title: "大号侧同拍照",
                    photo: []
                },
                {   
                    title: "小号侧同拍照",
                    photo: []
                }
            ],
            type_arr: {},
            jdwdMeg:{//非安卓环境下参数
                lon:'',
                lat:'',
                address:''
            },
            lon: '',    //安卓经度
            lat: '',    //安卓纬度
            photoLists1: [
                {
                    title: "不到位照片",
                    photo:[]
                }
            ],
            pohtoShowDW: {              //未到位拍照
            	type: 'cgxsIsDw',
            	img: true
            },
            noPlaceShow: false,         //0分钟不到位
            noTimeShow: false,          //0~3分钟不到位
            DWphoto: [],                //不到位拍照
            reason: '',                 //不到位原因
            stayTime: '',               // 到位秒时间
            noPlaceReasonFlag: false,	//不到位原因分析选择弹出模态
            noPlaceReasonColumns: [],	//不到位原因分析
            params: this.$route.query   //获取地址栏参数
        }
    },
    created(){
        this.isShow();          //是否显示结束-用于已完成时查看
        this.getSelectName();   //获取任务接单人
        this.photoVerification();
    },                
    methods: {
        // 头部两侧按钮点击事件
        headerClick(e){
            this.endTower() //结束该杆塔任务
        },
        headerClickHome(){
            this.$router.push({name:"index"})
        },
        // 获取任务接单人
        getSelectName(){
            let user = getSession("peopleName")
            this.taskPersonnel = user;
        },
        // 初始化缺陷数据
        initQuanxian(e){
            this.type_arr = e;
        },
        // 八大缺陷的点击事件
		quexianClick(e){
            this.$router.push({
                name:'cgxs_quexianList',
                query: {
                    name: e.allName,
                    flag: e.FLAG,
                    taskStatus: this.params.status, //任务状态用于已完成时隐藏可操作接口
                    towerId: this.params.towerId,
                    towerTaskId: this.params.xsTowerTaskId,
                    taskId: this.params.taskId,
                    defectType: e.index,
                    xsTowerTaskId: this.params.xsTowerTaskId,
                    lineTowerId: this.params.lineTowerId,
                    lineId: this.params.lineId,
                    jdUserId: this.params.jdUserId,
                    phoType: this.params.phoType,
                    status: this.params.status,
                    taskStatus: this.params.taskStatus, 
                    towerStatus: this.params.towerStatus,
                    needMeasure: this.params.needMeasure,
                    index: this.params.index,    //taskType任务类型
                    lineName: this.params.lineName,
					towerName: this.params.towerName
                }
            })
        },
        // 隐患列表
        goYhList() {
            this.$router.push({
				name:'cgxs_yinhuanList',
				query : {
					xsId: this.params.xsId,
					page: this.params.page,
					size: this.params.size,
					towerId: this.params.towerId,
					punchtype: this.params.punchtype,
					lineId: this.params.lineId,
					lineName: this.params.lineName,
					towerName: this.params.towerName,
					taskId: this.params.taskId,
					taskStatus: this.params.taskStatus, 
					towerStatus: this.params.towerStatus,
					lineTowerId: this.params.lineTowerId,
					towerTaskId: this.params.towerTaskId
	        	}
			})
        },
        // 测量
        goClArea() {
            let query = {
				xsId: this.params.xsId,
				xsTowerTaskId: this.params.xsTowerTaskId,
				lineTowerId: this.params.lineTowerId,
				taskId: this.params.taskId,
				towerId: this.params.towerId,
				towerName: this.params.towerName,
				lineId: this.params.lineId,
				jdUserId: this.params.jdUserId,
				lineName: this.params.lineName,
				phoType: this.params.phoType,//拍照必拍项
				status:  this.params.status,
				taskEndTime: this.params.taskEndTime,//任务结束时间
				taskStatus:this.params.taskStatus,//任务状态
				towerStatus: this.params.towerStatus,//杆塔状态
				needMeasure: this.params.needMeasure,//测量限制
				towerTaskId: this.params.towerTaskId,
				towerName: this.params.towerName,
				index: this.params.index,	//taskType任务类型
				page: this.params.page,
				size: this.params.size
            }
            this.$router.push({
                path:'cgxs_ceLiang',
				query
			})
            // this.$router.push('/cgxs_ceLiang');
        },
        // 是否显示结束-用于已完成时查看
        isShow(){
            if(this.params.towerStatus ==2){
                this.isEnd = false;
                this.isHome = true;
            }
        },
        //取消任务
        cancelTask() {
            let userId = JSON.parse(sessionStorage.getItem('userMeg')).id;  // 获取登录人ID
            var params = {};
            params.xsTowerTaskId = this.$route.query.xsTowerTaskId;
            params.acceptTask = 0;// 0 表示取消接单
            // 如果没有人接单则可取消
            if(this.params.jdUserId ==null){
                callData('/xsTaskServer/ZcXsTowerTask/appXsTowerJd', 'put', params).then(res => {
                    if(res.success){
                        Toast(res.data);
                        let query = {
                            xsId: this.params.xsId,
                            xsTowerTaskId: this.params.xsTowerTaskId,
                            lineTowerId: this.params.lineTowerId,
                            taskId: this.params.taskId,
                            towerId: this.params.towerId,
                            towerName: this.params.towerName,
                            lineId: this.params.lineId,
                            jdUserId: this.params.jdUserId,
                            lineName: this.params.lineName,
                            phoType: this.params.phoType,
                            status: this.params.status,
                            taskEndTime: this.params.taskEndTime,//任务结束时间
                            taskStatus:this.params.taskStatus,//任务状态
                            towerStatus: this.params.towerStatus,//杆塔状态
                            needMeasure: this.params.needMeasure,//测量限制
                            towerTaskId: this.params.towerTaskId,
                            towerName: this.params.towerName,
                            index: this.params.index,	//taskType任务类型
                            page: this.params.page,
                            size: this.params.size
                        }
                        // this.$router.go(-1);
                        this.$router.push({
                            name:'cgxs_gantaXunshi',
                            query: {
                                xsId: this.params.xsId,
                                taskStatus: this.params.taskStatus,
                                page: this.params.page,
                                size: this.params.size,
                                index: this.params.index
                            }
                        })
                    }
                })
            }
            //  当登录人id和接单人id一样时，允许取消
            else if(this.params.jdUserId == userId){
        	 	callData('/xsTaskServer/ZcXsTowerTask/appXsTowerJd', 'put', params).then(res => {
                    if(res.success){
                        Toast(res.data);
                        // this.$router.go(-1);
                        this.$router.push({
                            name:'cgxs_gantaXunshi',
                            query: {
                                xsId: this.params.xsId,
                                taskStatus: this.params.taskStatus,
                                page: this.params.page,
                                size: this.params.size,
                                index: this.params.index
                            }
                        })
                    }
                })
            }
            //  否则提示不允许取消
            else {  
                this.alertMeg()
            }
        },
        // 不可取消任务提示
		alertMeg(){
			Dialog.alert({
				message: '您非当前任务接单人，不可取消'
			}).then(() => {
				// on close
			});
        },
        //stayTime= 0 不到位拍照
        noPlaceConfirm(){
            this.noPlace();
        },
        // 没到范围
        noPlace(){
            //巡视不到位必拍照
            let isDWphoto = this.photoLists1[0].photo.length;
            if(isDWphoto == 0){
                Dialog.alert({
					title: '提示',
					message: '请拍摄不到位照片！'
				}).then(() => {
					
				});
            	return
            }
            // 不到位原因必填
            if(this.reason.length==0){
                Toast('原因不能为空');
                return
            }
            var params = {};
            if(window.Cordova){
                params.jd = this.lon;//经度
                params.wd = this.lat;//纬度
            }
            else {
                params.jd = this.jdwdMeg.lon;//经度
                params.wd = this.jdwdMeg.lat;//纬度
            }
            params.xsTowerTaskId = this.$route.query.xsTowerTaskId;  //巡视杆塔ID
            params.isDw = '1';           //是否到位：到位0,不到位1
            params.reason = this.reason; //不到位原因
            callData('/xsTaskServer/ZcXsTowerTask/appXsTowerOver', 'put', params).then(res => {
                if(res.success){
                    // 是否有未完成的杆塔：如果有就跳转到任务列表；反之，跳到杆塔列表
                    if(res.data.check){
                        Toast(res.data.Message)
                        this.$router.push({
                            path: '/cgxs_xianluXunshi',
                            query: {
                                index: this.$route.query.index
                            }
                        })
                    }
                    else {
                        Toast(res.data.Message)
                        // this.$router.go(-1)
                        this.$router.push({
                            name:'cgxs_gantaXunshi',
                            query: {
                                xsId: this.params.xsId,
                                taskStatus: this.params.taskStatus,
                                page: this.params.page,
                                size: this.params.size,
                                index: this.params.index
                            }
                        })
                    }
                }
            })
        },
        //0~3分钟不到位 - 提示
        noTimeConfirm(){
            this.noTime();
        },
        // 没到时间
        noTime(){
            var params = {};
            if(window.Cordova){
                params.jd = this.lon;//经度
                params.wd = this.lat;//纬度
            }
            else {
                params.jd = this.jdwdMeg.lon;//经度
                params.wd = this.jdwdMeg.lat;//纬度
            }
            params.xsTowerTaskId = this.$route.query.xsTowerTaskId; //巡视杆塔ID
            params.isDw = '1';                                      //是否到位：到位0,不到位1
            params.reason =`该杆塔巡视时间为${this.stayTime}秒，不达标！`;//不到位原因
            callData('/xsTaskServer/ZcXsTowerTask/appXsTowerOver', 'put', params).then(res => {
                if(res.success){
                    Toast(res.data.Message);
                    // 是否有未完成的杆塔：如果有就跳转到任务列表；反之，跳到杆塔列表
                    if(res.data.check){
                        this.$router.push({
                            path: '/cgxs_xianluXunshi',
                            query: {
                                index: this.$route.query.index
                            }
                        })
                    }
                    else {
                        // this.$router.go(-1)
                        this.$router.push({
                            name:'cgxs_gantaXunshi',
                            query: {
                                xsId: this.params.xsId,
                                taskStatus: this.params.taskStatus,
                                page: this.params.page,
                                size: this.params.size,
                                index: this.params.index
                            }
                        })
                    }
                }
            })
        },
        // 取消
        Cancel(){
            this.noPlaceShow = false
            this.noTimeShow = false
        },
        // 不到位原因弹出框
		showNoPlaceReason(val){
            this.noPlaceReasonFlag = true;
            callData('/accountServer/CmDataDictionary/findDataType','get',{
				dataType:"1"
			}).then(data => {
				const noPlaceReasonArr = [];
				data.data.forEach(function(causeVal,i){
					noPlaceReasonArr.push(causeVal.DATA_NAME);
				})
                this.noPlaceReasonColumns = noPlaceReasonArr;
			})
		},
		//不到位原因选择模态框确认按钮点击事件
		noPlaceReasonChange(picker, values, index){
			this.reason = values;
		},
		noPlaceReasonConfirm(selected){
			this.reason = selected;
			this.noPlaceReasonFlag = false;
		},
		//不到位原因选择模态框取消按钮点击事件
		noPlaceReasonCancel(){
			this.noPlaceReasonFlag = false;
		},
        //结束该杆塔任务
        endTower(){
            Dialog.confirm({
                title: '结束杆塔任务',
                message: '确定要结束该杆塔任务吗？'
            }).then(() => {
                // on confirm
                this.photoVerification(1)
                if(window.Cordova){
                    getLocation(res => {
                        var jd = res.data.lon;
                        var wd = res.data.lat;
                        this.lon = jd;//经度
                        this.lat= wd;//纬度
                        var params = {};
                        params.jd = this.lon;//经度
                        params.wd = this.lat;//纬度
                        params.xsTowerTaskId = this.$route.query.xsTowerTaskId;  //巡视杆塔ID
                        params.isDw = '-1';           //是否到位：初始化传-1，到位0,不到位1
                        params.reason = '';       //不到位原因
                        callData('/xsTaskServer/ZcXsTowerTask/appXsTowerOver', 'put', params).then(res => {
                            if(res.success){
                                // 是否到位:如果stayTime(单位ms)的值大于等于3分钟则到位，否则不到位
                                if(res.data.stayTime){//不到位
                                    this.stayTime = (res.data.stayTime)/1000;
                                    let stayTimems = Number(res.data.stayTime); //ms
                                    if(stayTimems == 0){ //需求：stayTime=0 提示用户拍不到位照片并填写不到位原因
                                        this.noPlaceShow = true
                                    }
                                    else{ //需求：0<stayTime<3min 提示用户不到位
                                        this.noTimeShow = true
                                    }
                                }
                                else {//到位
                                    if(res.data.check){
                                        Toast(res.data.Message)
                                        this.$router.push({
                                            path: '/cgxs_xianluXunshi',
                                            query: {
                                                index: this.$route.query.index
                                            }
                                        })
                                    }
                                    else {
                                        Toast(res.data.Message)
                                        // this.$router.go(-1)
                                        this.$router.push({
                                            name:'cgxs_gantaXunshi',
                                            query: {
                                                xsId: this.params.xsId,
                                                taskStatus: this.params.taskStatus,
                                                page: this.params.page,
                                                size: this.params.size,
                                                index: this.params.index
                                            }
                                        })
                                    }
                                }
                            }
                            else {
                                Toast(res.data);
                            }
                        })
                    })
                }
                else{
                    var params = {};
                    params.xsTowerTaskId = this.$route.query.xsTowerTaskId;//巡视杆塔ID
                    params.jd = this.jdwdMeg.lon;            //经度 this.jdwdMeg.lat
                    params.wd = this.jdwdMeg.lat;            //纬度
                    params.isDw = '-1';                      //是否到位：初始化传-1，到位0,不到位1
                    params.reason = ''; //不到位原因
                    callData('/xsTaskServer/ZcXsTowerTask/appXsTowerOver', 'put', params).then(res => {
                        if(res.success){
                            if(res.data.stayTime){
                                this.stayTime = (res.data.stayTime)/1000; //s秒
                                let stayTimems = Number(res.data.stayTime); //ms
                                // 是否到位:如果stayTime(单位ms)的值大于等于3分钟则到位，否则不到位
                                if(stayTimems == 0){ //需求：stayTime=0 提示用户拍不到位照片并填写不到位原因
                                    this.noPlaceShow = true
                                }
                                else{ //需求：0<stayTime<3min 提示用户不到位
                                    this.noTimeShow = true
                                }
                            }
                            else {
                                if(res.data.check){
                                    Toast(res.data.Message)
                                    this.$router.push({
                                        path: '/cgxs_xianluXunshi',
                                        query: {
                                            index: this.$route.query.index
                                        }
                                    })
                                }
                                else {
                                    Toast(res.data.Message)
                                    // this.$router.go(-1)
                                    this.$router.push({
                                        name:'cgxs_gantaXunshi',
                                        query: {
                                            xsId: this.params.xsId,
                                            taskStatus: this.params.taskStatus,
                                            page: this.params.page,
                                            size: this.params.size,
                                            index: this.params.index
                                        }
                                    })
                                }
                            }
                        }
                        else {
                            Toast(res.data);
                        }
                    })
                }
            }).catch(() => {
                // on cancel
            });
        },
        // 杆塔必拍照
        photoVerification(e){
            let needPhoto = this.params.phoType;    //string 1,2,3
            if(needPhoto){
                let needPhotoArr = needPhoto.split(',');//array
                let photoListsArr =  this.photoLists; //array 拍照项
                needPhotoArr.forEach((item,index) => {
                    photoListsArr.forEach((v,i) => {
                        if(item==i+1) {
                            if(v.photo.length == 0){
                                this.asterisk.push(item);
                                if(e){
                                    Toast(v.title+'不能为空').reverse()
                                }
                            }
                        }
                    })
                });
            }
        },
        // 一键告警
        getSos(){
            Dialog.confirm({
                title: '告警',
                message: '确定要发出告警吗？'
            }).then(() => {
                // on confirm
                if(window.Cordova){
                    getLocation(res => {
                    	var params = {};
                        params.jd = res.data.lon;
                        params.wd = res.data.lat;
                        params.alarmAddress=res.data.address;  // 当前的地理位置
                        console.log('告警jd',res.data.lon);
                        console.log('告警wd',res.data.lat);
                        console.log('告警地址',res.data.address);
                        callData('/statisServer/CmAlarm/saveCmAlarm', 'post', params).then(res => {
                            if(res.success){
                            	Toast('告警发送成功');
                            }
                            else {
                            	Toast('告警发送失败');
                        	}
                        })
                    })
                }
                else{
                    var params = {};
                    params.jd = this.jdwdMeg.lon;            //经度 
                    params.wd = this.jdwdMeg.lat;            //纬度
                    params.alarmAddress = this.jdwdMeg.address;
                    callData('/statisServer/CmAlarm/saveCmAlarm', 'post', params).then(res => {
                        if(res.success){
                        	Toast('告警发送成功');
                            console.log('告警res',res);
                        }
                        else {
                            Toast('告警发送失败');
                        }
                    })
                }
            }).catch(() => {
                // on cancel
            });
        }
    }
}
</script>
